// 通用功能函数
document.addEventListener('DOMContentLoaded', function() {
    // 文件上传处理
    const fileInputs = document.querySelectorAll('input[type="file"]');
    
    fileInputs.forEach(input => {
        input.addEventListener('change', function(e) {
            const fileName = e.target.files[0]?.name;
            if (fileName) {
                const label = this.nextElementSibling;
                if (label && label.classList.contains('file-label')) {
                    label.textContent = fileName;
                }
            }
        });
    });

    // 表单提交处理
    const forms = document.querySelectorAll('form');
    
    forms.forEach(form => {
        form.addEventListener('submit', function(e) {
            const submitBtn = this.querySelector('button[type="submit"]');
            const loading = this.querySelector('.loading');
            
            if (submitBtn && loading) {
                submitBtn.disabled = true;
                loading.style.display = 'block';
            }
        });
    });

    // 拖拽上传功能
    const dropAreas = document.querySelectorAll('.upload-area');
    
    dropAreas.forEach(area => {
        ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
            area.addEventListener(eventName, preventDefaults, false);
        });
        
        function preventDefaults(e) {
            e.preventDefault();
            e.stopPropagation();
        }
        
        ['dragenter', 'dragover'].forEach(eventName => {
            area.addEventListener(eventName, highlight, false);
        });
        
        ['dragleave', 'drop'].forEach(eventName => {
            area.addEventListener(eventName, unhighlight, false);
        });
        
        function highlight() {
            area.style.background = '#e9ecef';
            area.style.borderColor = '#764ba2';
        }
        
        function unhighlight() {
            area.style.background = '#f8f9fa';
            area.style.borderColor = '#667eea';
        }
        
        area.addEventListener('drop', handleDrop, false);
        
        function handleDrop(e) {
            const dt = e.dataTransfer;
            const files = dt.files;
            const fileInput = area.querySelector('input[type="file"]');
            
            if (fileInput && files.length > 0) {
                fileInput.files = files;
                const event = new Event('change');
                fileInput.dispatchEvent(event);
            }
        }
    });
});

// 显示加载状态
function showLoading() {
    const loading = document.querySelector('.loading');
    if (loading) {
        loading.style.display = 'block';
    }
}

// 隐藏加载状态
function hideLoading() {
    const loading = document.querySelector('.loading');
    if (loading) {
        loading.style.display = 'none';
    }
}

// 显示结果
function showResult(resultHtml) {
    const resultArea = document.querySelector('.result-area');
    if (resultArea) {
        resultArea.innerHTML = resultHtml;
        resultArea.style.display = 'block';
    }
}

// 显示错误
function showError(message) {
    const resultArea = document.querySelector('.result-area');
    if (resultArea) {
        resultArea.innerHTML = `
            <div class="error-message" style="color: #dc3545; text-align: center; padding: 2rem;">
                <h3>错误</h3>
                <p>${message}</p>
            </div>
        `;
        resultArea.style.display = 'block';
    }
}